package com.kkb.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.pojo.Menu;
import com.kkb.pojo.ResultVO;
import com.kkb.service.MenuService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("menu")
@ResponseBody
public class MenuController {
    @Resource
    private MenuService menuService;


    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVO<Menu> queryByPage(Integer pageNum, Integer pageSize, String menuName) {
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 5;
        }
        PageInfo<Menu> info = menuService.queryByPage(pageNum, pageSize, menuName);
        return new ResultVO<>(info);
    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResultVO<Menu> queryById(@PathVariable("id") Integer menuId) {
        Menu menu = menuService.queryById(menuId);
        return new ResultVO<Menu>(menu);
    }

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResultVO<Menu> queryByMenuName(String menuName) {
        List<Menu> menuList = menuService.queryByMenuName(menuName);
        if (menuList.size() > 0) {
            return new ResultVO<>(500);
        } else {
            return new ResultVO<Menu>(200);
        }
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultVO<Menu> addMenu(Menu menu) {
        int i = menuService.addMenu(menu);
        if (i == 1) {
            return new ResultVO<>(200);
        } else {
            return new ResultVO<>(500, "添加失败");
        }
    }

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public ResultVO<Menu> deleteMenu(@PathVariable("id") Integer menuId) {
        int i = menuService.deleteMenu(menuId);
        if (i == 1) {
            return new ResultVO<>(200);
        } else {
            return new ResultVO<>(500, "删除失败");
        }
    }

    @RequestMapping(value = "", method = RequestMethod.DELETE)
    public ResultVO<Menu> deleteMenus(@RequestParam("ids[]") List<Integer> ids) {
        /*for (Integer id : ids) {
            System.out.println(id);
        }*/
        int i = menuService.deleteMenus(ids);
        if (i == ids.size()) {
            return new ResultVO<>(200);
        } else {
            return new ResultVO<>(500, "删除失败");
        }
    }

    @RequestMapping(value = "", method = RequestMethod.PUT)
    public ResultVO<Menu> updateMenu(Menu menu) {
        int i = menuService.updateMenu(menu);
        ResultVO<Menu> resultVO = new ResultVO<>();
        if (i == 1) {
            resultVO.setCode(200);
        } else if (i == -1) {
            resultVO.setCode(500);
            resultVO.setMsg("资源名重复");
        } else {
            resultVO.setCode(500);
            resultVO.setMsg("更新失败");
        }
        return resultVO;
    }
}
